package com.fjxd.ticketStore.web;

import com.fjxd.feign.clients.PlaneClient;
import com.fjxd.feign.clients.PlaneTicketClient;
import com.fjxd.feign.pojo.Plane;
import com.fjxd.ticketStore.pojo.TicketStore;
import com.fjxd.ticketStore.service.TicketStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.fjxd.ticketStore.util.TypeConstants.*;

/**
 * @author csw
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/ticketStore")
public class TicketStoreController {
    final
    PlaneTicketClient planeTicketClient;
    final
    PlaneClient planeClient;
    final
    TicketStoreService ticketStoreService;

    public TicketStoreController(PlaneTicketClient planeTicketClient, PlaneClient planeClient, TicketStoreService ticketStoreService) {
        this.planeTicketClient = planeTicketClient;
        this.planeClient = planeClient;
        this.ticketStoreService = ticketStoreService;
    }


    /**
     * 批量模拟生成机票库存
     */
    @PostMapping("createTicketStore")
    public void createTicketStore() {
        List<TicketStore> ticketStoreList = new ArrayList<>();
        planeTicketClient.getAll().forEach(ticket -> {
            Plane planeById = planeClient.getPlaneById(ticket.getPId());
            for (int i = 1; i <= ECONOMY; i++) {

                TicketStore ticketStore = new TicketStore();

                switch (i) {
                    case 1:
                        if (planeById.getFirstClass() != 0) {
                            ticketStore.setTicketId(ticket.getId());
                            ticketStore.setTicketType(FIRST);
                            ticketStore.setTicketTypeName("头等舱");
                            ticketStore.setTicketCount(planeById.getFirstClass());
                            ticketStore.setTicketPrice(ticket.getFirstPrice());
                            ticketStoreList.add(ticketStore);
                        }
                        break;
                    case 2:
                        ticketStore.setTicketId(ticket.getId());
                        ticketStore.setTicketType(BUSINESS);
                        ticketStore.setTicketTypeName("商务舱");
                        ticketStore.setTicketCount(planeById.getBusinessClass());
                        ticketStore.setTicketPrice(ticket.getBusinessPrice());
                        ticketStoreList.add(ticketStore);
                        break;
                    case 3:
                        ticketStore.setTicketId(ticket.getId());
                        ticketStore.setTicketType(ECONOMY);
                        ticketStore.setTicketTypeName("经济舱");
                        ticketStore.setTicketCount(planeById.getEconomyClass());
                        ticketStore.setTicketPrice(ticket.getEconomyPrice());
                        ticketStoreList.add(ticketStore);
                        break;
                    default:
                        log.error("未知错误");
                }

            }
        });
        ticketStoreService.saveBatch(ticketStoreList);
    }

    @PutMapping("/deduct/{ticketId}/{ticketType}")
    public void ticketDeduct(@PathVariable String ticketId, @PathVariable Integer ticketType, @RequestParam(defaultValue = "1") Integer ticketCount) {
        ticketStoreService.ticketDeduct(ticketId, ticketType, ticketCount);
    }

    @PutMapping("/goBack/{aircraftTicketId}/{ticketCount}")
    public void ticketGoBack(@PathVariable Integer aircraftTicketId, @PathVariable Integer ticketCount) {
        ticketStoreService.ticketGoBack(aircraftTicketId, ticketCount);
    }

    @GetMapping("/getTicketStore/{ticketId}")
    public List<TicketStore> getTicketStore(@PathVariable Integer ticketId) {
        return ticketStoreService.getTicketCount(ticketId);
    }


}
